/*
 * calculation_server_test.cpp
 *
 *  Created on: 21/06/2011
 *      Author: Yotam and Gilaad
 */

#include "../polynomial.h"
#include "mtmtest.h"
#include "../matrix.h"
#include <stdarg.h>

Matrix initMatrix( int rows, int columns, ... ) {
	Matrix result( rows, columns );

	va_list vl;
	va_start( vl, columns );

	for( int r = 1; r <= rows; ++r ) {
		for( int c = 1; c <= columns; ++c ) {
			double cell = va_arg(vl,double);
			result(r, c) = cell;
		}
	}
	va_end( vl );
	return result;
}

bool testPolynomial(){

	Matrix A = initMatrix(2,2,1.5,2.5,3.5,4.5);
	Matrix B = initMatrix(2,1,5.5,6.5);
	Matrix C = initMatrix(4,1,5.5,8.5,-1.0,5.0);
	Polynomial job1(1,5,A,B);
	Polynomial job2(2,25,A,C);
	Matrix resultAB = initMatrix(2,2,15.25,16.25,22.75,34.75);
	Matrix resultAC = initMatrix(2,2,352.25,481.25,673.75,929.75);
	Matrix AC = job2.execute();
	Matrix AB = job1.execute();
	//should be equal
	ASSERT_EQUALS(AB, resultAB);
	ASSERT_EQUALS(AC, resultAC);

	Matrix D = initMatrix(2,2,1.5,1.5,1.5,2.5);
	Matrix E = initMatrix(1,4,2.5,2.5,2.5,3.5);
	job1 = Polynomial(3, 56, A, D);
	job2 = Polynomial(4, 41, A, E);
	//should fail
	ASSERT_THROW(Job::JobFailed,job1.execute());
	ASSERT_THROW(Job::JobFailed,job2.execute());

	return true;
}

int main(){
	RUN_TEST(testPolynomial);
	return 0;
}
